package org.example.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.example.mapper.ScheduleMapper;
import org.example.pojo.PageBean;
import org.example.pojo.R;
import org.example.pojo.Schedule;
import org.example.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ScheduleServiceImpl implements ScheduleService {

   @Resource
   private ScheduleMapper scheduleMapper;

   @Override
   public R page(int pageSize, int currentPage) {
      //调用之前，先设置分页数据（当前是第几页，每页显示多少个）
      PageHelper.startPage(currentPage, pageSize);

      //查询
      List<Schedule> scheduleList = scheduleMapper.queryList();

      //将查询数据封装到一个PageInfo的分页实体类，
      PageInfo<Schedule> info = new PageInfo<>(scheduleList);

      //配置PageBean
      PageBean<Schedule> pageBean = new PageBean<>(currentPage,pageSize,info.getTotal(),info.getList());

      R ok = R.ok(pageBean);
      return ok;
   }

   @Override
   public R remove(int id) {
      int rows = scheduleMapper.delete(id);
      if (rows > 0) {
         return R.ok(null);
      }
      return R.fail(null);
   }

   @Override
   public R save(Schedule schedule) {
      int rows = scheduleMapper.save(schedule);
      if (rows > 0) {
         return R.ok(null);
      }
      return R.fail(null);
   }

   @Override
   public R update(Schedule schedule) {
      if (schedule.getId() == null) {
         return R.fail("核心参数为null,无法修改");
      }
      int rows = scheduleMapper.update(schedule);
      if (rows > 0) {
         return R.ok(null);
      }
      return R.fail(null);
   }
}
